package com.hangxin.commons.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
/**
 * 时间工具类
 * @author 顾德地
 *
 */
public class DateUtil {

	public static String getMonthFirstDay(){
		//获取当前月第一天：
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(c.getTime());
        return first;
	}
	
	public static String getCurrentMonth(){
		//获取当前月第一天：
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();    
        String first = format.format(c.getTime());
        return first;
	}
	
	public static String getMonthLastDay(){
		//获取当前月最后一天：
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();    
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = format.format(ca.getTime());
        return last;
	}
	
	/**
	 * 得到某日期前一天日期
	 * @param d
	 * @return
	 */
	public static Date getBeforeDate(Date d){  
	Date date = null; 
	Calendar calendar = Calendar.getInstance();    
	calendar.setTime(date);    
	calendar.add(Calendar.DAY_OF_MONTH,-1);    
	date = calendar.getTime();   
	return date; 
	}
	
	/**
	 * 得到某日期后一天日期
	 * 顾德地
	 */
	public static Date getAfterDate(Date date){  
		//Date date = null;  
		Calendar calendar = Calendar.getInstance();    
		calendar.setTime(date);    
		calendar.add(Calendar.DAY_OF_MONTH,1);    
		date = calendar.getTime();    
		return date; 
	}
	/**
	 * 前一月第一天和最后一天
	 * @return
	 */
	public static Map<String, String> findLastMonth() {
		  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		  Calendar cal = Calendar.getInstance();
		  GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		  Calendar calendar = Calendar.getInstance();
		  calendar.setTime(new Date());
		  calendar.add(Calendar.MONTH, -1);
		  Date theDate = calendar.getTime();
		  gcLast.setTime(theDate);
		  gcLast.set(Calendar.DAY_OF_MONTH, 1);
		  String day_first_prevM = df.format(gcLast.getTime());
		  StringBuffer str = new StringBuffer().append(day_first_prevM);
		  //.append(" 00:00:00");
		  day_first_prevM = str.toString();
          //System.out.println("前一月第一天"+day_first_prevM);
		  calendar.add(cal.MONTH, 1);
		  calendar.set(cal.DATE, 1);
		  calendar.add(cal.DATE, -1);
		  String day_end_prevM = df.format(calendar.getTime());
		  StringBuffer endStr = new StringBuffer().append(day_end_prevM);
		  //.append(" 23:59:59");
		  day_end_prevM = endStr.toString();
		  //System.out.println("前一月最后一天"+day_end_prevM);
		  Map<String, String> map = new HashMap<String, String>();
		  map.put("first", day_first_prevM);
		  map.put("end", day_end_prevM);
		  return map;
		 }
	/**
	 * 获取上月第一天
	 * 顾德地
	 */
	public static String getBefMonthFirst(){  
		Calendar curCal = Calendar.getInstance();
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		curCal.set(Calendar.DAY_OF_MONTH, 1);
		Date beginTime = curCal.getTime();
		String start = datef.format(beginTime);   
		return start; 
	}
	
	/**
	 * 获取上月最后一天
	 * 顾德地
	 */
	public static String getBefMonthEnd(){  
		Calendar calendar = Calendar.getInstance();    
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		// 获取当月上个月最后一天
		calendar.set(Calendar.DATE, 0);
		calendar.roll(Calendar.DATE, -1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		Date endTime = calendar.getTime();
		String end = datef.format(endTime);
		return end;
	}
}
